<?php

class AddVideo extends MY_Controller {

    public function __construct() {
        parent::__construct();
        
        if($this->session->userdata('user_type') != 1 || $this->session->userdata('user_type') == null){
            show_error("You have no permission here");
        }
    }

    function index() {      
        $this->initForm();
        $this->data['action'] = 'add';
        $this->data['main_content'] = 'add_video';
        $this->load->view('includes/template', $this->data);
    }
    
    function initForm(){
        
        $this->data['main_title'] = "Add Instruction Media";
          //set value(blank coz of new data)
        $video = array(
            'id' => '',
            'video_name' => '',
            'course' => '',
            'video_path' => '',
            'video_detail' => '',
            'video_type' => ''
        );
        $this->data['video'] = $video;
            
        //get dropdownlist for course
        $this->load->Model('Course_Model');
        $courseList = $this->Course_Model->getCourseList();
        $this->data['courseList'] = $courseList;
        
        $videoName = array(
              'id'        => 'videoName',
              'name'        => 'videoName'
            );
        
        $videoDetail = array(
              'id'        => 'videoDetail',
              'name'        => 'videoDetail'
            );
        $videoPath = array(
                'id'        => '$videoPath',
              'name'        => '$videoPath'
            );
        
        $videoTypeList = array("avi" => "avi",
                        "mp4" => "mp4",
                        "flv" => "flv",
                        "wmv" => "wmv");
        
        $this->data['videoName'] = $videoName;
        $this->data['videoDetail'] = $videoDetail;
        $this->data['videoPath'] = $videoPath;
        $this->data['videoTypeList'] = $videoTypeList;
        
        $this->data['action'] = '';
    }

    function UploadVideo() {
        $this->initForm();
        $course = $this->input->post('course');
        $videoName = $this->input->post('videoName');
        $videoDetail = $this->input->post('videoDetail');
        $videoType = $this->input->post('type');

        $video = array(
                'VIDEO_NAME' => $videoName,
                'COURSE_ID' => $course,
                'VIDEO_PATH' => "upload/video/" . $videoName . "." . $videoType,
                'VIDEO_DETAIL' => $videoDetail,
                'VIDEO_TYPE' => $videoType
            );
        
        $this->load->model('Video_Model');
        if($this->Video_Model->insertVideo($video)){
            echo "<script>
                    alert('Video has been saved successfull!!!');
                    window.location.href='AddVideo';
                  </script>";
        }else{
            echo "<script>
                    alert('Save failed!!!');
                    window.location.href='AddVideo';
                  </script>";
        }

    }
}

?>
